package com.fastadmin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fastadmin.mapper.impl.ResourceMapper;
import com.fastadmin.pojo.Resource;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequiredArgsConstructor
public class WebController extends BaseController {
    public static final LineCaptcha lineCaptcha = null;
    private static final Logger logger = LoggerFactory.getLogger(WebController.class);


    private final ResourceMapper resourceMapper;

    @GetMapping(value = {"/admin/home/index", "/"})
    public String index(Model model) {
        Authentication authentication = getAuthentication();
        if (authentication.getPrincipal().equals("anonymousUser")) {
            logger.debug("匿名用户不能访问！～{}", "请重新验证");
            return "redirect:/login";
        }
        Collection<? extends GrantedAuthority> authorities = getAuthentication().getAuthorities();
        List<Resource> resources = resourceMapper.findTypeResourceAndRole(CollUtil.toList(0, 1), authorities);
        //拿到所有pid=0的父菜单
        List<Pair<Resource, List<Resource>>> collect = resources
                .stream()
                .filter(resource -> resource.getPid() == 0)
                .collect(Collectors.toList())
                .stream()
                .map(resource -> {
                    ArrayList<Resource> subResource = Lists.newArrayList();
                    for (Resource oldResource : resources) {
                        if (ObjectUtil.equal(oldResource.getPid(), resource.getId())) {
                            subResource.add(oldResource);
                        }
                    }
                    Pair<Resource, List<Resource>> resourceListPair = new Pair<>(resource, subResource);
                    return resourceListPair;
                })
                .collect(Collectors.toList());

        model.addAttribute("menus", collect);
        return "index";
    }


}
